package com.steam.splace;


import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.Intent;
import android.content.Loader;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import bo.IsConnected;

import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import thread.DoThread;
import thread.LoadLoggedIn;
import thread.DoThreadGet;

import dao.DataAccess;
import entity.User;
public class  MainActivity extends Activity {
	private final int login_tag =1;
    private final int register_tag = 2;
    private final int category_tag =4;
    private final int signup_tag =5;
    private final int viewprofile_tag = 6;
    String token="";
    private final int getfavorite_tag =9;
    public static DefaultHttpClient client = new DefaultHttpClient();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		IsConnected ic=new IsConnected(this);
		if(ic.isConnectedToInternet()==true){
			DataAccess da=new DataAccess(this);
			int uid=-1;
			try {
				da.open();
				User u=da.getUserInUse();
				if(u!=null){
				uid=u.getId();
				
				token=u.getAccessToken();
				}
				else uid=-1;
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				//e.printStackTrace();
				uid=-1;
			}
			finally{da.close();}
			System.out.println("uid from main"+uid);
			SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
			uid = prefs.getInt("uid", -1);		  
		     System.out.println("Uid from log:"+uid);
			if(uid!=-1){
				prefs.edit().putString("token", token);
				prefs.edit().putInt("uid", uid);
				prefs.edit().commit();}
				LoadLoggedIn load=new LoadLoggedIn(this);
				load.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
			}
		else{
			this.setContentView(R.layout.activity_main);
			Toast toast=Toast.makeText(this, "Vui lòng kiểm tra đường truyền mạng!",
				Toast.LENGTH_LONG);
		toast.setGravity(Gravity.CENTER, 0, 0);
		toast.show();}
	}
	public void onClick(View view) {
		int id = view.getId();
		if (id == R.id.btLogin) {
			Button bt=(Button)this.findViewById(R.id.btLogin);
			bt.setEnabled(false);
			TextView txtEmail = (TextView) findViewById(R.id.txtEmail);
			TextView txtPassword = (TextView) findViewById(R.id.txtPassword);
			//if(txtEmail.getText()!=null&&!txtEmail.getText().toString().trim().equals("")&&txtPassword.getText()!=null&&!txtPassword.getText().toString().trim().equals("")){
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("email", txtEmail.getText().toString()));
			params.add(new BasicNameValuePair("password",txtPassword.getText().toString()));
			Log.v("email",txtEmail.getText().toString());
			Log.v("password",txtPassword.getText().toString());
			ArrayList l=new ArrayList();
			l.add(login_tag);
			l.add(params);
			DoThread thread=new DoThread(MainActivity.this);
			thread.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR,l);
		} else if (id == R.id.btOpenForgotPasswordActivity) {
			Intent forgotIntent = new Intent(this, ForgotPasswordActivity.class);
			startActivity(forgotIntent);
		} else if (id == R.id.btOpenSearchPlaceActivity) {
			Intent searchIntent = new Intent(this, SearchActivity.class);
			startActivity(searchIntent);
		} else if (id == R.id.btOpenSignUpActivity) {
			Intent signUpIntent = new Intent(this, SignUpActivity.class);
			startActivity(signUpIntent);
		}
		}
	/*@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		doThreadGet thread1=new doThreadGet(this);
		ArrayList params1=new ArrayList();
		params1.add(getfavorite_tag);
		DataAccess da=new DataAccess(this);
		int uid=-1;
		try {
			da.open();
			uid=da.getUserInUse().getId();
			da.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		params1.add(uid);
		params1.add(menu);
		thread1.execute(params1);
		return true;
	}*/
	/*@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
		if(item.getItemId()==R.id.menuSearch){
			if(!this.getClass().getSimpleName().equals("SearchActivity")){
				
				Intent searchIntent = new Intent(this, SearchActivity.class);
			startActivity(searchIntent);
			}
		}
		else if(item.getItemId()==R.id.menuProfile){
			if(!this.getClass().getSimpleName().equals("ProfileActivity")){
			Intent profileIntent = new Intent(this, ProfileActivity.class);
			startActivity(profileIntent);}
		}
		else if(item.getItemId()!=R.id.menuSearch&&item.getItemId()!=R.id.menuProfile&&item.getItemId()!=10){
			Intent placeIntent=new Intent(this, ViewPlaceActivity.class);
			placeIntent.putExtra("pid", item.getItemId());
			startActivity(placeIntent);
		}
		return true;
	    }*/
	//System.out.println(contextParent.getClass().getSimpleName());
}
